package com.gxa.pingan.controller;

import com.gxa.pingan.common.JsonResult;
import com.gxa.pingan.pojo.Evaluate;
import com.gxa.pingan.service.EvaluateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @version fi
 * @Author lonny
 * @Date 2021/10/26 14:11
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/evaluate")
@Transactional(rollbackFor = Throwable.class)
@Api(value = "评价操作的接口")
public class EvaluateController {


    private final EvaluateService evaluateService;


    public EvaluateController(EvaluateService evaluateService) {
        this.evaluateService = evaluateService;
    }

    @ApiOperation(value = "添加评价的接口方法,默认评价是可见状态true,id自动注入,不需要传参")
    @PostMapping("/insert")
    @ApiImplicitParam(name = "evaluate", value = "evaluate", paramType = "body")
    public Map<String, Object> add(@RequestBody Evaluate evaluate) throws Exception {
        evaluateService.addEvaluate(evaluate);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/update")
    @ApiOperation("更改司机评论是否可见的接口,点击之后即更改状态")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query", required = true)
    public Map<String, Object> update(String id) throws Exception {
        evaluateService.updateEvaluate(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/getList")
    @ApiOperation("查询司机评价的接口/分页默认每页10个数据,用户名/用户电话/司机电话/订单编号支持模糊查询,时间查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "object", value = "输入需要查询的值", paramType = "query"),
            @ApiImplicitParam(name = "fromPayTime", value = "fromPayTime", paramType = "query"),
            @ApiImplicitParam(name = "toPayTime", value = "toPayTime", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "page", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "size", paramType = "query", required = true)
    })
    public Map<String, Object> getList(String object, String fromPayTime, String toPayTime, Integer page, Integer size) throws Exception {
        List<Map<String, Object>> evaluateList = evaluateService.getEvaluateList(object, fromPayTime, toPayTime, page, size);
        int total = evaluateService.size(object, fromPayTime, toPayTime);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(evaluateList);
        jsonResult.put("total", total);
        return jsonResult.getDataMap();
    }

    @ApiOperation("评价详情页")
    @PostMapping("/details")
    @ApiImplicitParam(name = "id", value = "评价id", required = true)
    public Map<String, Object> getDetails(String id) throws Exception {
        Map<String, Object> details = evaluateService.details(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(details);
        return jsonResult.getDataMap();
    }

    @ApiOperation("删除评价,ids为被删除订单的集合,可以多个，例1,2，则删除1，2对应的评价")
    @PostMapping("/delete")
    @ApiImplicitParam(name = "ids", value = "ids", paramType = "query", required = true, type = "String")
    public Map<String, Object> delete(String ids) throws Exception {
        System.out.println(ids);
        int i = evaluateService.deleteEvaluate(ids);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("删除条数", i);
        return jsonResult.getDataMap();
    }
}
